#!/bin/bash

#------------------------------------------------------------------------------
#Filename:	75333-nfs-1.sh
#Version:	1.1
#Date:		2013/10/18
#Author:	yuanhui.shi
#Email:		yuanhui.shi@cs2c.com.cn
#Summary:	02.04网络服务-05NFS-01字符模式（典型配置）-服务器和客户端是同一台机器
#Notes:		NFS服务典型配置
#Copyright:	China Standard Software Co.,Ltd.
#History:
#		Version 1.0,2013/11/18
#		-The first one
#-----------------------------------------------------------------------------


PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

source ../../../lib/Echo.sh
source ../../../lib/XmlParse.sh
source ./lib/cmd.sh

function CleanData ()
{
  EchoInfo "关闭服务器端的nfs服务，删除共享目录"
  stop_daemon nfs
  echo '' >/etc/exports
  umount /mnt/rw &> /dev/null
  umount /mnt/ro &> /dev/null
  rm -rf /mnt/*
  rm -rf /nfs-test
  EchoInfo "75333-nfs-1.sh执行完毕"
}

trap "CleanData" EXIT INT

#################################
#
# start, stop, restart service
#
#################################
if command -v systemctl >/dev/null 2>&1; then
    HAVE_SYSTEMCTL=1
else
    HAVE_SYSTEMCTL=0
fi

##! @TODO: start service
##! @AUTHOR: huijing.hei
##! @VERSION: 1.0 
function start_daemon()
{
        if [ $HAVE_SYSTEMCTL -eq 1 ]; then
                systemctl start $1.service > /dev/null 2>&1
        else
                service $1 start > /dev/null 2>&1
        fi
}

function stop_daemon()
{
        if [ $HAVE_SYSTEMCTL -eq 1 ]; then
                systemctl stop $1.service > /dev/null 2>&1
        else
                service $1 stop > /dev/null 2>&1
        fi
}

function status_daemon()
{
        if [ $HAVE_SYSTEMCTL -eq 1 ]; then
                systemctl status $1.service > /dev/null 2>&1
        else
                service $1 status > /dev/null 2>&1
        fi
}

function restart_daemon()
{
        if [ $HAVE_SYSTEMCTL -eq 1 ]; then
                systemctl restart $1.service > /dev/null 2>&1
        else
                service $1 restart > /dev/null 2>&1
 fi
}

#set enviroment

stop_daemon iptables
stop_daemon nfs

#create two directories
mkdir -p /nfs-test/readonly
touch /nfs-test/readonly/file1
mkdir -p /nfs-test/rw
chmod -R 777 /nfs-test/readonly /nfs-test/rw /nfs-test

#Modify the main conf file of nfs service
echo "/nfs-test/readonly *(ro)" >>/etc/exports
echo "/nfs-test/rw *(rw)" >>/etc/exports

#Start nfs service
start_daemon nfs

#查看nfs共享目录
EchoInfo "本机检验nfs服务器是否配置成功"
  
test -n "`showmount -e 127.0.0.1 |grep /nfs-test/readonly`" -a -n "`showmount -e 127.0.0.1 |grep /nfs-test/rw`" 
EchoResult "客户端可以看见nfs共享目录"

#客户端在/mnt目录下新建两个目录：rw和ro
mkdir /mnt/rw
mkdir /mnt/ro


#客户端挂载只读目录/nfs-test/readonly
mount 127.0.0.1:/nfs-test/readonly /mnt/ro
EchoInfo "检验客户端能否在只读目录下进行写操作"
echo "This is a test!" > /mnt/ro/file1
if [ $? -ne 0 ]
then
    echo "Pass|客户端不能在只读目录下进行写操作"
else   
    echo "Fail|客户端不能在只读目录下进行写操作"
fi

#客户端卸载只读目录
EchoInfo "客户端能否成功卸载只读目录"
umount /mnt/ro
test -z `ls /mnt/ro`
EchoResult "客户端成功卸载只读目录"


#客户端挂载读写目录/nfs-test/rw
mount 127.0.0.1:/nfs-test/rw /mnt/rw
EchoInfo "检验客户端能否在读写目录下进行写操作"
echo "This is a test!" > /mnt/rw/file1
EchoResult "客户端在读写目录下进行写操作"

#客户端卸载读写目录
EchoInfo "客户端能否成功卸载读写目录"
umount /mnt/rw
test -z `ls /mnt/rw`
EchoResult "客户端成功卸载读写目录"

